<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" plain icon="el-icon-refresh" @click="tableRefresh"></el-button>
				<el-button type="primary" icon="el-icon-plus" @click="table_add"></el-button>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTableGdshop ref="table" :apiObj="apiObj" :column="tableColumn" stripe remoteSort remoteFilter>
				<el-table-column label="操作" fixed="right" align="right" width="220">
					<template #default="scope">
						<el-button type="text" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
						<el-button type="text" size="small" @click="table_del(scope.row, scope.$index)">删除</el-button>
					</template>
				</el-table-column>
			</scTableGdshop>
		</el-main>
		<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
	</el-container>
</template>

<script>
import saveDialog from './save'
export default {
	name: "setting-configs",
	components: {
		saveDialog
	},
	data() {
		return {
			apiObj: this.$API.gdshop.configs,
			dialog: {
				save: false
			},
			tableColumn: [
				{
					prop: "config_name",
					label: "参数名称"
				},
				{
					prop: "config_key",
					label: "键名"
				},
				{
					prop: "config_value",
					label: "数据"
				},
				{
					prop: "remark",
					label: "备注",
				}
			]
		}
	},
	methods: {
		tableRefresh() {
			this.$refs.table.refresh()
		},
		table_add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		table_edit(row) {
			console.log(row)
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		table_del(row) {
			this.$confirm(`确定删除选中的 ${row.config_name}  吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				const loading = this.$loading();
				var res = await this.apiObj.delete({ ids: [row.id] });
				loading.close();
				if (res.code === 0) {
					this.$message.success("操作成功")
					this.tableRefresh()
				}
				else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}).catch(() => {

			})
		},
		//本地更新数据
		handleSuccess(data, mode){
			console.log(data, mode);
			this.tableRefresh();
		}
	}
}
</script>

<style scoped>

</style>
